package Client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainUIActionListener implements ActionListener ,MouseListener{

	User user;
	/**监听的MainUI*/
	private MainUI mainUI;
	/**鼠标拖动窗口*/
	private Point point;

	MainUIActionListener(MainUI mainUI,User user){
		this.mainUI=mainUI;
		this.user=user;
		MainUIMotionHandler();
	}

	//鼠标拖动窗口时进行响应
	private void MainUIMotionHandler(){
		this.mainUI.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				point = e.getPoint();
			}
		});
		// 面板的鼠标拖曳事件中移动窗体
		this.mainUI.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent e) {
				Point newpoint = e.getLocationOnScreen();
				mainUI.setLocation(newpoint.x - point.x, newpoint.y - point.y);
			}
		});

		new Thread() {// 创建新线程
			public void run() {
				int screenwidth, screenheight;
				Toolkit kit = Toolkit.getDefaultToolkit();
				Dimension screensize = kit.getScreenSize();
				screenwidth = (int) screensize.getWidth();
				screenheight = (int) screensize.getHeight();
				for (int i = 0; i <= 680; i += 85) {// 循环拉伸窗体
					mainUI.setBounds(screenwidth / 2 - 300 / 2, screenheight / 2 - i / 2, 300, i);// 不断设置窗体大小与位置
					try {
						Thread.sleep(5);// 线程休眠10毫秒
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
			}
		}.start();// 启动线程
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==mainUI.bExit){
			mainUI.tray.remove(mainUI.icon);
			System.exit(0);
		}
		if(e.getSource()==mainUI.bMin){
			mainUI.setVisible(false);
		}
		if(e.getSource()==mainUI.mit){
			mainUI.tray.remove(mainUI.icon);
			System.exit(0);
		}
		if(e.getSource()==mainUI.bSearch){
			String searchInfo=mainUI.searchText.getText().trim();
			if (!"搜索：联系人".equals(searchInfo) && !searchInfo.equals("")) {
				if (user.haveThisFriend(searchInfo)){
					JOptionPane.showMessageDialog(null,"搜索成功","搜索结果",JOptionPane.WARNING_MESSAGE);
					System.out.println("true");
				}
				mainUI.searchText.setText("搜索：联系人");			}
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==mainUI.icon){
			mainUI.setVisible(true);
		}
		else if(e.getSource()==mainUI.addFriend && !user.isAddFriendUIexist()){
			user.setAddFriendUIexist(true);//窗口存在不需要时点击按钮不需要再建窗口
			new AddFriendUI(user);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			// 获取焦点
			mainUI.requestFocus();
		}

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if(e.getSource()==mainUI.bExit){
			mainUI.bExit.setContentAreaFilled(true);
			mainUI.bExit.setBackground(Color.red);
		}
		if(e.getSource()==mainUI.bSearch){
			mainUI.bSearch.setContentAreaFilled(true);
			mainUI.bSearch.setBackground(Color.WHITE);
		}
		if(e.getSource()==mainUI.bMin){
			mainUI.bMin.setContentAreaFilled(true);
			mainUI.bMin.setBackground(Color.white);
		}
		if(e.getSource()==mainUI.bContact){
			mainUI.bContact.setContentAreaFilled(true);
			mainUI.bContact.setBackground(Color.white);
		}
		if(e.getSource()==mainUI.addFriend){
			mainUI.addFriend.setContentAreaFilled(true);
			mainUI.addFriend.setBackground(Color.white);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		if(e.getSource()==mainUI.bExit){
			mainUI.bExit.setContentAreaFilled(false);
		}
		if(e.getSource()==mainUI.bSearch){
			mainUI.bSearch.setContentAreaFilled(false);
		}
		if(e.getSource()==mainUI.bMin){
			mainUI.bMin.setContentAreaFilled(false);
		}
		if(e.getSource()==mainUI.bContact){
			mainUI.bContact.setContentAreaFilled(false);
		}
		if(e.getSource()==mainUI.addFriend){
			mainUI.addFriend.setContentAreaFilled(false);
		}
	}
}
